home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 121 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.7 KB

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: clamage@Eng.sun.com (Steve Clamage)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: FW: Inherent C++ problem?
  5. Date: 20 Jan 1996 16:22:25 GMT
  6. Organization: Sun Microsystems Inc., Mountain View, CA
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <4dpne3$si8@engnews1.Eng.Sun.COM>
  9. References: <01BAE696.8C249300@dino.int.com>
  10. NNTP-Posting-Host: taumet.eng.sun.com
  11. Content-Type: text
  12. X-Nntp-Posting-Host: taumet.eng.sun.com
  13. X-Newsreader: NN version 6.5.0 #21 (NOV)
  14. Content-Length: 1940
  15. X-Lines: 58
  16. Originator: clamage@taumet
  17.  
  18. Eugene Lazutkin <eugene@int.com> writes:
  19.  
  20. >----------
  21. >From:     Max Motovilov[SMTP:max@int.com]
  22. >Sent:     Friday, January 19, 1996 4:01 PM
  23. >To:     'Eugene Lazutkin'
  24. >Subject:     (for comp.std.c++)  Inherent C++ problem?
  25.  
  26. >Looks like Subj to me, but maybe I misunderstand something or overlooking a 
  27. >simple workaround...
  28.  
  29. >Here is the example:
  30.  
  31. >    struct    Complex
  32. >    {
  33. >        double re, im;
  34.  
  35. >        Complex( double r, double i ) : re( r ), im( i ) {}
  36. >        Complex( const Complex& c ) : re( c.re ), im( c.im ) {}
  37. >    };
  38.  
  39. >    Complex    operator + ( const Complex& a, const Complex& b )
  40. >    {
  41. >        return Complex( a.re+b.re, a.im+b.im );
  42. >    }
  43.  
  44. >It is all right by now. Consider the following use then:
  45.  
  46. >    Complex a( 1, 0 ), b( 0, 1 );
  47. >    Complex c( a+b );
  48.  
  49. >In the last line 3 objects of type Complex are constructed instead of 1, ...
  50.  
  51. >This is inefficient to the last extreme, moreover it cannot even be 
  52. >optimized out since any copy-constructor can theoretically have non-local 
  53. >side effects....
  54.  
  55. That is not true. Under some circumstances extra temporary objects
  56. may be optimized away, even when the copy constructor has global
  57. side effects. (That means that correctness of your program should
  58. not depend on how many temporaries get generated.)
  59.  
  60. In the example, the result returned from a+b can be constructed
  61. directly in the parameter used to build 'c'. (I'm assuming this
  62. example is deliberately simplified; you would not really need to 
  63. write a copy construtor for this class. But lets speak to the more
  64. general point where copying is not trivial.)
  65.  
  66. Here is part of the relevent section in the draft standard:
  67.  
  68. 12.2 Temporary objects
  69. While evaluating an expression, it might be necessary or convenient
  70. for an implementation to generate temporary objects to hold values
  71. resulting from the evaluation of the expression's subexpressions.
  72. During this evaluation, precisely when such temporaries are
  73. introduced is unspecified.
  74. --
  75. Steve Clamage, stephen.clamage@eng.sun.com
  76.  
  77. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  78.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  79.   is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
  80.  
  81.